#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>


void bit_move(int fd, int *addr)
{
    
    //BMP图片中前54个字节是图片信息，因此需要跳过
    //BMP图片的像素点为BGR 
    //LCD屏幕的像素点为ARGB
    //需要换位置
    char buf_ch[800*480*3];
    memset(buf_ch, 0, 800*480*3);
    
    lseek(fd, 54, SEEK_SET);

    int ret = read(fd, buf_ch, 800*480*3);
    if(ret < 0)
    {
        perror("read image fail");
        exit(1);
    }

     //行
    int x = 0;
    //列
    int y = 0;

  for(y=0;y<480;y++)
    {
        
        for(x=0;x<800;x++) 
            *(addr+(479-y)*800+x) = buf_ch[(y*800+x)*3+0] |
                                buf_ch[(y*800+x)*3+1]<<8 |
                                buf_ch[(y*800+x)*3+2]<<16;
    }

}

int main(int agrc, char **argv)
{
    int fd_lcd = open("/dev/fb0", O_RDWR);
    if(fd_lcd < 0)
    {
        perror("open /dev/fb0 fail");
        exit(1);
    }

    int fd = open("IKUN.bmp", O_RDWR);
    if(fd < 0)
    {
        perror("open image fail");
        exit(1);
    }

    int fd1 = open("dingzhen.bmp", O_RDWR);
    if(fd1 < 0)
    {
        perror("open image fail");
        exit(1);
    }


    int *addr = (int *)mmap(NULL, 800*480*4, PROT_READ | PROT_WRITE, MAP_SHARED, fd_lcd, 0);
    if(addr == NULL)
    {
        perror("mmap fd_lcd fail");
        exit(1);
    }
    int *addr1 = (int *)mmap(NULL, 800*480*4, PROT_READ | PROT_WRITE, MAP_SHARED, fd_lcd, 0);
    if(addr == NULL)
    {
        perror("mmap fd_lcd fail");
        exit(1);
    }

    while(1)
    {
    bit_move(fd, addr);
    sleep(2);
    bit_move(fd1,addr1);
    sleep(2);
    }
   
    munmap(addr, 800*480*4);
    close(fd_lcd);
    close(fd);
    close(fd1);
    return 0;
}
